package com.borqs.samples;

import android.content.Context;
import android.os.ServiceManager;
import android.os.Parcel;
import android.os.IBinder;
import android.util.Log;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class AlarmServiceProvider{
    public static final String TAG = "AlarmServiceProvider";
    static final int TRANSACTION_BORQS_BASE = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1000);
    static final int TRANSACTION_BORQS_getStatsInfo = (TRANSACTION_BORQS_BASE + 1);
    static final int TRANSACTION_BORQS_enableStats = (TRANSACTION_BORQS_BASE + 2);
    static final int TRANSACTION_BORQS_enableAction = (TRANSACTION_BORQS_BASE + 3);
    static final int TRANSACTION_BORQS_getActionInfo = (TRANSACTION_BORQS_BASE + 4);
    static final int TRANSACTION_BORQS_addActionRule = (TRANSACTION_BORQS_BASE + 5);
    static final int TRANSACTION_BORQS_removeActionRule = (TRANSACTION_BORQS_BASE + 6);

    public static Map<String,Object> getStatsInfo(){
        Map<String,Object> result = new HashMap<String,Object>();
        IBinder client = (IBinder)ServiceManager.getService(Context.ALARM_SERVICE);
        if(client == null) {
            Log.e(TAG,"in getStatsInfo,the service proxy is null");
            return null;
        }
        boolean ret = false;
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        try {
            data.writeInterfaceToken("android.app.IAlarmManager");
            ret = client.transact(TRANSACTION_BORQS_getStatsInfo,data,reply,0);
            if(ret) {
                boolean enabled = reply.readInt()==1?true:false;
                long startTime = reply.readLong();
                long stopTime = reply.readLong();
                Map<String,Integer> pkgCount = new HashMap<String,Integer>();
                while(reply.readInt() != -1){
                    String pkg = reply.readString();//pkg
                    int count = reply.readInt();//count
                    pkgCount.put(pkg,count);
                }

                result.put("enabled",enabled);
                result.put("startTime",startTime);
                result.put("stopTime",stopTime);
                result.put("pkgCount",pkgCount);//Map<String,Integer>
            }
            else Log.i(TAG,"in getStatsInfo,AlarmManagerService's transact return false");
        } catch (Exception e) {
            Log.e(TAG,"getStatsInfo transaction exception: "+e);
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }
    public static Map<String,Object> getActionInfo(){
        Map<String,Object> result = new HashMap<String,Object>();
        IBinder client = (IBinder)ServiceManager.getService(Context.ALARM_SERVICE);
        if(client == null) {
            Log.e(TAG,"in getActionInfo,the service proxy is null");
            return null;
        }
        boolean ret = false;
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        try {
            data.writeInterfaceToken("android.app.IAlarmManager");
            ret = client.transact(TRANSACTION_BORQS_getActionInfo,data,reply,0);
            if(ret) {
                boolean enabled = reply.readInt()==1?true:false;
                long startTime = reply.readLong();
                long stopTime = reply.readLong();
                Map<String,String[]> pkgActions = new HashMap<String,String[]>();
                while(reply.readInt() != -1){
                    String pkg = reply.readString();//pkg
                    String[] array = new String[2];
                    reply.readStringArray(array);
                    pkgActions.put(pkg,array);
                }
                result.put("enabled",enabled);
                result.put("startTime",startTime);
                result.put("stopTime",stopTime);
                result.put("pkgAction",pkgActions);//Map<String,String[]>
            }
            else Log.i(TAG,"in getActionInfo,AlarmManagerService's transact return false");
        } catch (Exception e) {
            Log.e(TAG,"getActionInfo transaction exception: "+e);
        } finally {
            reply.recycle();
            data.recycle();
        }
        return result;
    }
    public static void enableStats(boolean enabled){
        IBinder client = (IBinder)ServiceManager.getService(Context.ALARM_SERVICE);
        if(client == null) {
            Log.e(TAG,"in enableStats,the service proxy is null");
            return;
        }
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        try {
            data.writeInterfaceToken("android.app.IAlarmManager");
            data.writeInt(enabled?1:0);
            client.transact(TRANSACTION_BORQS_enableStats,data,reply,0);
        } catch (Exception e) {
            Log.e(TAG,"enableStats transaction exception: "+e);
        } finally {
            reply.recycle();
            data.recycle();
        }
    }
    public static void enableAction(boolean enabled){
        IBinder client = (IBinder)ServiceManager.getService(Context.ALARM_SERVICE);
        if(client == null) {
            Log.e(TAG,"in enableAction,the service proxy is null");
            return;
        }
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        try {
            data.writeInterfaceToken("android.app.IAlarmManager");
            data.writeInt(enabled?1:0);
            client.transact(TRANSACTION_BORQS_enableAction,data,reply,0);
        } catch (Exception e) {
            Log.e(TAG,"enableAction transaction exception: "+e);
        } finally {
            reply.recycle();
            data.recycle();
        }
    }
    public static void addActionRule(String pkg,String actionName,List<String> params){
        IBinder client = (IBinder)ServiceManager.getService(Context.ALARM_SERVICE);
        if(client == null) {
            Log.e(TAG,"in addActionRule,the service proxy is null");
            return;
        }
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        try {
            data.writeInterfaceToken("android.app.IAlarmManager");
            data.writeString(pkg);
            data.writeString(actionName);
            data.writeInt(params.size());
            data.writeStringArray(toArray(params));

            client.transact(TRANSACTION_BORQS_addActionRule,data,reply,0);
        } catch (Exception e) {
            Log.e(TAG,"addActionRule transaction exception: "+e);
        } finally {
            reply.recycle();
            data.recycle();
        }
    }
    public static void removeActionRule(String pkg){
        IBinder client = (IBinder)ServiceManager.getService(Context.ALARM_SERVICE);
        if(client == null) {
            Log.e(TAG,"in removeActionRule,the service proxy is null");
            return;
        }
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        try {
            data.writeInterfaceToken("android.app.IAlarmManager");
            data.writeString(pkg);
            client.transact(TRANSACTION_BORQS_removeActionRule,data,reply,0);
        } catch (Exception e) {
            Log.e(TAG,"removeActionRule transaction exception: "+e);
        } finally {
            reply.recycle();
            data.recycle();
        }
    }
    private static String[] toArray(List<String> t){
        String[] a = new String[t.size()];
        for(int i = 0;i<t.size();i++){
            a[i] = t.get(i);
        }
        return a;
    }
}
